package model

import (
	"context"
	"service/ctrl"
	"service/model/mod"

	gpb "gitee.com/cloudapex/protos/crm_base/pb"
	"google.golang.org/grpc"
)

var Proj proj

func init() {
	ctrl.GCon.RegistClienter(mod.GCONN_CRM_BASE, func(c *grpc.ClientConn) {
		Proj.cli = gpb.NewProjectSerClient(c)
	})
}

type proj struct {
	cli gpb.ProjectSerClient
}

//项目列表(新老兼容)
func (this *proj) GetProjectList(ctx context.Context, in *gpb.ProjectListReq) (*gpb.ProjectListRes, error) {
	return this.cli.GetProjectList(ctx, in)
}

func (this *proj) GetProjNames(ctx context.Context, cid int64) (map[int64]string, error) {
	cacheprojNames := map[int64]string{}
	rsp, err := this.cli.GetProjectList(ctx, &gpb.ProjectListReq{
		Page:    1,
		Count:   1000,
		DesClum: 1,
		IsDes:   2,
		Project: &gpb.Project{CId: cid, State: 1},
	})
	if err != nil {
		return cacheprojNames, err
	}
	for _, it := range rsp.Project {
		cacheprojNames[it.ProId] = it.Name
	}
	return cacheprojNames, nil
}
func (this *proj) GetProjIdNames(ctx context.Context, cid int64) (map[int64]string, error) {
	cacheprojIdNames := map[int64]string{}
	rsp, err := this.cli.GetProjectList(ctx, &gpb.ProjectListReq{
		Page:    1,
		Count:   1000,
		DesClum: 1,
		IsDes:   2,
		Project: &gpb.Project{CId: cid, State: 1},
	})
	if err != nil {
		return cacheprojIdNames, err
	}
	for _, it := range rsp.Project {
		cacheprojIdNames[it.Id] = it.Name
	}
	return cacheprojIdNames, nil
}

func (this *proj) GetProjNameNames(ctx context.Context, cid int64) (map[string]int64, error) {
	cacheprojIdNames := map[string]int64{}
	rsp, err := this.cli.GetProjectList(ctx, &gpb.ProjectListReq{
		Page:    1,
		Count:   1000,
		DesClum: 1,
		IsDes:   2,
		Project: &gpb.Project{CId: cid, State: 1},
	})
	if err != nil {
		return cacheprojIdNames, err
	}
	for _, it := range rsp.Project {
		cacheprojIdNames[it.Name] = it.Id
	}
	return cacheprojIdNames, nil
}
